package com.duan.user.controller;

import com.duan.entity.PageResult;
import com.duan.entity.Result;
import com.duan.entity.StatusCode;
import com.duan.user.pojo.Address;
import com.duan.user.service.AddressService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户地址
 */
@RestController
@RequestMapping("/Address")
public class AddressController {
    @Autowired
    private AddressService addressService;


    /**
     * 查询全部
     * @param
     * @return
     */
    @GetMapping("/selectAll")
    public Result<List<Address>> selectAll(){
        return new Result<>("查询成功", addressService.selectAll());
    }


    /**
     * 条件查询
     * @param address
     * @return
     */
    @PostMapping("/selectList")
    public Result< List<Address> > select(@RequestBody Address address){
        return new Result<>("条件查询成功", addressService.selectList(address));
    }

    /**
     * 新增
     * @param address
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Address address){
        addressService.insert(address);
        return new Result<>("新增成功");
    }


    /**
     * 删除
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam String ids){
        addressService.delete(ids);
        return new Result<>("删除成功");
    }


    /**
     * 修改
     * @param address
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Address address){
        addressService.update(address);
        return new Result<>("修改成功");
    }

    /**
     * 分页搜索实现
     * @param page 默认值1
     * @param size 默认值10
     * @return
     */
    @GetMapping(value = "/pageAll" )
    public Result pageAll(@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
        Page<Address> pageList = addressService.pageAll(page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true, StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 分页条件搜索实现
     * @param address
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/pageList" )
    public Result pageList(@RequestBody Address address,@RequestParam(defaultValue = "1")  Integer page, @RequestParam(defaultValue = "10") Integer size){
        Page<Address> pageList = addressService.pageList(address,page, size);
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }
}

